package com.decent.ejfadmin.finance.service.impl;


import cn.decent.component.shiro.entity.Admin;
import cn.decent.component.shiro.service.AdminService;
import com.decent.ejfadmin.common.bean.*;
import com.decent.ejfadmin.common.enums.ErrorCode;
import com.decent.ejfadmin.common.enums.SupRedisKey;
import com.decent.ejfadmin.common.enums.WithdrawLogStatusEnum;
import com.decent.ejfadmin.common.enums.WithdrawLogTypeEnum;
import com.decent.ejfadmin.common.exception.OperateFailException;
import com.decent.ejfadmin.common.exception.SupException;
import com.decent.ejfadmin.common.util.Constants;
import com.decent.ejfadmin.common.util.DingTalkMessagePush;
import com.decent.ejfadmin.common.util.WithHoldDes;
import com.decent.ejfadmin.config.PlatConfig;
import com.decent.ejfadmin.finance.dao.SupplyDailySheetDao;
import com.decent.ejfadmin.finance.dao.SupplyFinanceDao;
import com.decent.ejfadmin.finance.request.RebateUserQueryParam;
import com.decent.ejfadmin.finance.request.WithdrawRecordQueryParam;
import com.decent.ejfadmin.finance.response.dto.SupplyDailySheetDto;
import com.decent.ejfadmin.finance.response.dto.WithdrawRecordDto;
import com.decent.ejfadmin.finance.service.SupplyFinanceService;
import com.decent.ejfadmin.finance.service.WithdrawService;
import com.decent.ejfadmin.user.bean.Const;
import com.decent.ejfadmin.user.dao.UserDao;
import com.decent.ejfadmin.utils.SimpleCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 供货系统资金管理
 *
 * @author 王元鑫
 * @date 2016/8/6
 */
@Service
@Slf4j
public class SupplyFinanceServiceImpl implements SupplyFinanceService {
    @Autowired
    private SupplyFinanceDao supplyFinanceDao;
    @Autowired
    private SupplyDailySheetDao supplyDailySheetDao;
    @Autowired
    private AdminService adminService;
    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private SupplyFinanceService supplyFinanceService;
    @Autowired
    private PlatConfig platConfig;
    @Autowired
    private UserDao userDao;
    @Autowired
    private DingTalkMessagePush dingTalkMessagePush;

    /**
     * 修改供货余额、供货明细
     *
     * @param supplyConsumption 资金明细
     * @return 当前余额（修改前）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BigDecimal modifyBalanceConsumption(SupplyConsumption supplyConsumption) {
        int row;
        BigDecimal currentBalance;
        SupUser supUser = new SupUser();
        supUser.setUserId(supplyConsumption.getUserId());
        //获取当前余额、版本号
        SupplyBalance balance = querySupplyBalanceVersion(supplyConsumption.getUserId());
        //佣金
        if (supplyConsumption.getType() == SupplyConsumption.REBATE) {
            //当前佣金余额
            currentBalance = balance.getCommision();
            if (currentBalance.add(supplyConsumption.getMoney()).compareTo(BigDecimal.ZERO) < 0) {
                throw new OperateFailException("{" + supplyConsumption.getUserId() + "}|{" + supplyConsumption.getOrderId() + "}佣金余额变动后小于0");
            }
            supplyConsumption.setBalance(currentBalance.add(supplyConsumption.getMoney()));
            //修改余额
            row = supplyFinanceDao.modifyCommisionBalance(balance, supplyConsumption.getMoney().doubleValue(), supplyConsumption.getConsumeType());
            if (row != 1) {
                throw new OperateFailException("{" + supplyConsumption.getUserId() + "}|{" + supplyConsumption.getOrderId() + "}修改供货余额失败");
            }
            //返佣
            supplyConsumption.setType(1);
            //供卡
        } else {
            //当前供卡余额
            currentBalance = balance.getBalance();

            if (currentBalance.add(supplyConsumption.getMoney()).compareTo(BigDecimal.ZERO) < 0) {
                throw new OperateFailException("{" + supplyConsumption.getUserId() + "}|{" + supplyConsumption.getOrderId() + "}供卡余额变动后小于0");
            }
            supplyConsumption.setBalance(currentBalance.add(supplyConsumption.getMoney()));
            //修改余额
            row = supplyFinanceDao.modifyBalance(balance, supplyConsumption.getMoney().doubleValue(), supplyConsumption.getConsumeType());
            if (row != 1) {
                throw new OperateFailException("{" + supplyConsumption.getUserId() + "}|{" + supplyConsumption.getOrderId() + "}修改供货余额失败");
            }
            supplyConsumption.setType(0);
        }

        //结算类型增加消费明细
        if (supplyConsumption.getConsumeType() != SupplyConsumption.WITHDRAW_TYPE) {
            row = supplyFinanceDao.createSupplyConsumption(supplyConsumption);
            if (row != 1) {
                throw new OperateFailException("{" + supplyConsumption.getUserId() + "}|{" + supplyConsumption.getOrderId() + "}新增消费明细异常");
            }
        }

        return currentBalance;
    }

    /**
     * 查询供货余额
     *
     * @param userId 会员ID
     * @return SupplyBalance
     */
    @Override
    public SupplyBalance querySupplyBalanceVersion(Long userId) {
        SupplyBalance balance = supplyFinanceDao.querySupplyBalanceVersion(userId);
        if (balance == null) {
            supplyFinanceDao.createSupplyBalance(userId);
            return new SupplyBalance(userId);
        }
        return balance;
    }

    /**
     * 查询提现记录分页数据
     *
     * @param param
     * @return
     */
    @Override
    public Page findWithdrawRecordPage(WithdrawRecordQueryParam param) {
        int total = supplyFinanceDao.queryWithdrawRecordCount(param);
        param.getPageUnit().setTotal(total);
        if (total == 0) {
            param.getPageUnit().setResultList(new ArrayList<>());
        } else {
            List<WithdrawRecordDto> list = supplyFinanceDao.queryWithdrawRecordPage(param);
            list.forEach(t -> {
                if (StringUtils.equalsIgnoreCase(Const.ALIPAY, t.getWithdrawChannel())) {
                    t.setBankNo(t.getAlipay());
                } else {
                    t.setBankNo(WithHoldDes.decodeValue(t.getBankSalt(), t.getBankNo()));
                }
            });
            param.getPageUnit().setResultList(list);
        }
        return param.getPageUnit();
    }

    /**
     * 审核提现记录
     *
     * @param recordId
     * @param auditstate
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageBean checkWithdrawRecord(Long recordId, Integer auditstate) throws Exception {
        Admin admin = adminService.getCurrentAdmin();
        if (admin == null) {
            throw new SupException("请登录后再操作");
        }
        if (null == recordId || null == auditstate) {
            throw new SupException(ErrorCode.PARAM_ERROR, "参数有误recordId  " + recordId + " auditstate " + auditstate);
        }
        Object autoAudit = SimpleCacheUtil.getObject(SupRedisKey.AUTO_AUDIT.toString());
        if (Objects.nonNull(autoAudit)) {
            return MessageBean.fail("自动审核中不可审核提现记录");
        }
        WithdrawRecord dbWithdrawRecord = supplyFinanceDao.findWithdrawRecordById(recordId);
        if (Objects.isNull(dbWithdrawRecord)) {
            return MessageBean.fail("操作的数据不存在");
        }
        Integer logState = WithdrawLogStatusEnum.AUDIT_SUCCESS.getStatus();
        //如果是审核失败
        if (Constants.CHECK_FAIL.equals(auditstate)) {
            logState = WithdrawLogStatusEnum.AUDIT_FAIL.getStatus();
            dbWithdrawRecord.setState(Constants.FAIL_STATE);
            withdrawService.modifyWithdraw(dbWithdrawRecord);
            log.info("[{}]将提现记录[{}]审核失败", admin, recordId);
        }
        if (supplyFinanceDao.checkWithdrawRecord(recordId, auditstate) != 1) {
            return MessageBean.fail();
        }

        //添加提现露操作日志
        WithdrawLog withdrawLog = new WithdrawLog(admin.getName(), recordId, logState, WithdrawLogTypeEnum.USER_WITHDRAWALS);
        int error = supplyFinanceDao.addWithdrawLog(withdrawLog);
        if (error == 0) {
            log.info("添加提现记录操作失败[{}]", withdrawLog);
        }
        return MessageBean.success();
    }

    /**
     * 再次转账
     *
     * @param recordId
     * @return
     */
    @Override
    public void resetTransfer(Long recordId) throws Exception {
        Admin admin = adminService.getCurrentAdmin();
        if (admin == null) {
            throw new SupException("请登录后再操作");
        }
        if (null == recordId) {
            throw new SupException(ErrorCode.PARAM_ERROR, "参数有误recordId");
        }
        WithdrawRecord dbWithdrawRecord = supplyFinanceDao.findWithdrawRecordById(recordId);
        // 4:异常
        if (!Constants.EXCEPTION_STATE.equals(dbWithdrawRecord.getState())) {
            throw new SupException(ErrorCode.OPERATE_FAIL, " 只能处理异常订单");
        }
        dbWithdrawRecord.setFlowNo("004AG" + System.currentTimeMillis() + RandomStringUtils.randomAlphanumeric(9).toUpperCase());
        int count = supplyFinanceDao.resetTransfer(dbWithdrawRecord);
        if (count != 1) {
            throw new SupException(ErrorCode.OPERATE_FAIL, recordId + " 处理失败");
        }

        //添加提现操作日志
        WithdrawLog withdrawLog = new WithdrawLog(admin.getName(), recordId, WithdrawLogStatusEnum.RE_TRANSFER.getStatus(), WithdrawLogTypeEnum.USER_WITHDRAWALS);
        int error = supplyFinanceDao.addWithdrawLog(withdrawLog);
        if (error == 0) {
            log.info("添加提现记录操作失败[{}]", withdrawLog);
        }
        log.info("[{}]将提现记录[{}]再次转账", admin, recordId);
    }

    /**
     * 处理失败
     *
     * @param recordId 订单Id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dealFail(Long recordId) throws Exception {
        Admin admin = adminService.getCurrentAdmin();
        if (admin == null) {
            throw new SupException("请登录后再操作");
        }
        if (null == recordId) {
            throw new SupException(ErrorCode.PARAM_ERROR, "参数有误recordId");
        }
        WithdrawRecord withdrawRecord = supplyFinanceDao.findWithdrawRecordById(recordId);
        int state;
        if (withdrawRecord.getState() == Constants.SUCCESS_STATE.intValue()) {
            state = Constants.BACK_STATE;
        } else {
            throw new SupException(ErrorCode.PARAM_ERROR, "只能处理成功订单 " + withdrawRecord.getOrderId());
        }
        int count = supplyFinanceDao.dealFail(recordId, state, withdrawRecord.getState());
        if (count != 1) {
            throw new SupException(ErrorCode.OPERATE_FAIL, recordId + " 处理失败");
        }
        //查询消费明细
        List<SupplyConsumption> consumptions = supplyFinanceDao.querySupplyConsumption(withdrawRecord.getOrderId(), withdrawRecord.getUserId());
        if (consumptions == null) {
            throw new OperateFailException("{" + withdrawRecord.getOrderId() + "} 无此无法修改消费明细");
        }
        for (SupplyConsumption consumption : consumptions) {
            //提现成功->提现失败  提现手续费->提现手续费失败
            if (consumption.getConsumeType().equals(SupplyConsumption.SERVICE_TYPE)) {
                int row = supplyFinanceDao.modifySupplyConsumption(consumption.getId(), SupplyConsumption.SERVICE_TYPE_FAIL);
                if (row != 1) {
                    throw new OperateFailException("{" + withdrawRecord.getOrderId() + "} 提现手续费消费明细修改失败");
                }
            } else {
                int row = supplyFinanceDao.modifySupplyConsumption(consumption.getId(), SupplyConsumption.WITHDRAW_FAIL);
                if (row != 1) {
                    throw new OperateFailException("{" + withdrawRecord.getOrderId() + "} 成功提现消费明细修改失败");
                }
            }
        }
        //添加提现操作日志
        WithdrawLog withdrawLog = new WithdrawLog(admin.getName(), withdrawRecord.getId(), WithdrawLogStatusEnum.DEAL_SUCCESS_TO_FAIL.getStatus(), WithdrawLogTypeEnum.USER_WITHDRAWALS);
        int error = supplyFinanceDao.addWithdrawLog(withdrawLog);
        if (error == 0) {
            log.info("添加提现记录操作失败[{}]", withdrawLog);
        }
        try {
            SupUser user = userDao.queryUserById(withdrawRecord.getUserId());
            if (user.getSupInfo() != null && StringUtils.isNotBlank(user.getSupInfo().getOpenId())) {
                DbWithdrawRecord dbWithdrawRecord = supplyFinanceDao.queryDbWithdrawRecord(withdrawRecord.getOrderId());
                NotifyWeChatBean notifyWeChatBean = new NotifyWeChatBean();
                notifyWeChatBean.setOpenid(user.getSupInfo().getOpenId());
                notifyWeChatBean.setLoginAccount(user.getAccount());
                //兼容支付宝提现
                if (SupplyConsumption.BANK.equals(dbWithdrawRecord.getWithdrawChannel())) {
                    notifyWeChatBean.setWithdrawAccount(WithHoldDes.decodeValue(dbWithdrawRecord.getBankSalt(), dbWithdrawRecord.getTransferNo()));
                } else {
                    notifyWeChatBean.setWithdrawAccount(dbWithdrawRecord.getAlipay());
                }
                notifyWeChatBean.setMoney(dbWithdrawRecord.getPrice());
                notifyWeChatBean.setStartTime(DateFormatUtils.format(dbWithdrawRecord.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
                notifyWeChatBean.setOrderId(withdrawRecord.getOrderId());
                notifyWeChatBean.setStatus(Constants.FAIL_STATE);
                notifyWeChatBean.setRealName(dbWithdrawRecord.getCertName());
                dingTalkMessagePush.withdrawMessagePushingToWeiChat(notifyWeChatBean);
            }
        } catch (Exception e) {
            log.warn("提现订单[{}]推送会员异常[{}],", withdrawRecord, e.getMessage(), e);
        }
        log.info("[{}]将提现记录[{}]处理为失败", admin, withdrawRecord, recordId, withdrawRecord.getState());
    }

    /**
     * 自动审核开关
     *
     * @param status
     * @return
     */
    @Override
    public void changeAutoAuditStatus(Integer status) throws SupException {
        if (null == status) {
            throw new SupException(ErrorCode.PARAM_ERROR, "参数有误status");
        }
        if (Constants.STATE_OPEN.intValue() == status) {
            SimpleCacheUtil.setObject(SupRedisKey.AUTO_AUDIT.toString(), SupRedisKey.AUTO_AUDIT.toString());
        } else {
            SimpleCacheUtil.removeObject(SupRedisKey.AUTO_AUDIT.toString());
        }

    }

    /**
     * 查询返利用户信息统计分页数据
     *
     * @param param
     * @return
     */
    @Override
    public Page findRebateUserPage(RebateUserQueryParam param) {
        int total = supplyFinanceDao.queryRebateUserCount(param);
        param.getPageUnit().setTotal(total);
        if (total == 0) {
            param.getPageUnit().setResultList(new ArrayList<>());
        } else {
            param.getPageUnit().setResultList(supplyFinanceDao.queryRebateUserPage(param));
        }
        return param.getPageUnit();
    }

    /**
     * 自动审核获取状态
     *
     * @return
     */
    @Override
    public boolean getAutoAuditStatus() {
        Object autoAudit = SimpleCacheUtil.getObject(SupRedisKey.AUTO_AUDIT.toString());
        if (Objects.isNull(autoAudit)) {
            return false;
        }
        return true;
    }

    /**
     * 分页查询供货日报表
     *
     * @param pageUnit    分页组件
     * @param startTime   查询开始时间
     * @param endTime     查询结束时间
     * @param userAccount 会员编号
     * @return list
     */
    @Override
    public Page querySupplyDailySheetPage(Page pageUnit, String startTime, String endTime, String userAccount) {
        int total = supplyDailySheetDao.querySupplyDailySheetCount(startTime, endTime, userAccount);
        pageUnit.setTotal(total);
        if (total == 0) {
            pageUnit.setResultList(new ArrayList<>());
        } else {
            pageUnit.setResultList(supplyDailySheetDao.querySupplyDailySheetPage(pageUnit, startTime, endTime, userAccount));
        }
        return pageUnit;
    }

    /**
     * 供货日报表导出
     *
     * @param startTime   查询开始时间
     * @param endTime     查询结束时间
     * @param userAccount 会员编号
     * @return
     */
    @Override
    public void downLoad(String startTime, String endTime, String userAccount, HttpServletResponse response) {
        int total = supplyDailySheetDao.querySupplyDailySheetCount(startTime, endTime, userAccount);
        BufferedWriter bufferedWriter = null;
        try {
            ServletOutputStream servletOutputStream = response.getOutputStream();
            response.setHeader("Content-disposition", "attachment;filename=" + System.currentTimeMillis() + ".csv");
            Page page = new Page();
            page.setSize(5000);
            page.setTotal(total);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(servletOutputStream, "GBK"));
            bufferedWriter.write("日期,会员编号,会员名称,期初余额,期末余额,差额,提现至支付宝,提现至银行,提现手续费," +
                    "移动入账金额,联通入账金额,电信入账金额,入账金额,移动成功面值,联通成功面值," +
                    "电信成功面值,汇总面值,补结金额(三网),提现失败手动退款,服务费");
            bufferedWriter.newLine();
            while (page.getPage() <= page.getMaxPage()) {
                List<SupplyDailySheetDto> list = supplyDailySheetDao.querySupplyDailySheetPage(page, startTime, endTime, userAccount);
                int size = list.size();
                StringBuffer data = new StringBuffer(size * 100);
                for (int i = 0; i < size; i++) {
                    data.append(list.get(i));
                }
                bufferedWriter.write(StringUtils.replace(data.toString(), "null", ""));
                page.setPage(page.getPage() + 1);
            }
            bufferedWriter.flush();
        } catch (Exception e) {
            log.warn("供货日报表导出出错", e);
        } finally {
            IOUtils.closeQuietly(bufferedWriter);
        }
    }

    /**
     * 会员提现记录导出
     *
     * @param param
     * @return
     */
    @Override
    public void withdrawRecordDownLoad(WithdrawRecordQueryParam param, HttpServletResponse response) {
        ServletOutputStream servletOutputStream;
        BufferedWriter bufferedWriter = null;
        Page page = new Page();
        page.setSize(5000);
        param.setPageUnit(page);
        param.setTimeType(false);
        try {
            servletOutputStream = response.getOutputStream();
            int total = supplyFinanceDao.queryWithdrawRecordCount(param);
            page.setTotal(total);
            response.setHeader("Content-disposition", "attachment;filename=" + System.currentTimeMillis() + ".csv");
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(servletOutputStream, "GBK"));
            bufferedWriter.write("申请订单号,流水号,会员编号,会员名称,认证姓名,提现渠道,提现金额,提现类型," +
                    "审核状态,提现状态,申请时间,处理时间,备注");
            bufferedWriter.newLine();
            while (page.getPage() <= page.getMaxPage()) {
                List<WithdrawRecordDto> list = supplyFinanceDao.queryWithdrawRecordPage(param);
                int size = list.size();
                StringBuffer data = new StringBuffer(size * 100);
                for (int i = 0; i < size; i++) {
                    data.append(list.get(i));
                }
                bufferedWriter.write(StringUtils.replace(data.toString(), "null", ""));
                page.setPage(page.getPage() + 1);
            }
            bufferedWriter.flush();
        } catch (Exception e) {
            log.warn("会员提现记录导出出错", e);
        } finally {
            IOUtils.closeQuietly(bufferedWriter);
        }
    }

    /**
     * 对异常、申请中状态的申请人工更改其状态
     *
     * @param withdrawRecord 提现类
     * @param oldState       页面订单提现状态
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageBean updateWithdrawState(WithdrawRecord withdrawRecord, Integer oldState) throws Exception {
        Admin admin = adminService.getCurrentAdmin();
        if (admin == null) {
            return MessageBean.fail("请登录后再操作!");
        }
        WithdrawRecord dbWithdrawRecord = supplyFinanceDao.findWithdrawRecordById(withdrawRecord.getId());
        if (dbWithdrawRecord == null) {
            return MessageBean.fail("未查询到该记录");
        }
        if (!dbWithdrawRecord.getState().equals(oldState)) {
            return MessageBean.fail("当前订单状态已经发生变化,不可修改!");
        }
        //只允许处理1(审核通过)的订单
        if (!dbWithdrawRecord.getAuditstate().equals(1)) {
            return MessageBean.fail("该订单当前审核状态不允许进行此操作");
        }
        if (dbWithdrawRecord.getState() != Constants.EXCEPTION_STATE.intValue()
                && dbWithdrawRecord.getState() != Constants.DEALING_STATE.intValue()) {
            return MessageBean.fail("该订单当前状态不允许进行此操作");
        }
        if (dbWithdrawRecord.getState() == Constants.DEALING_STATE.intValue() && dbWithdrawRecord.getEndTime().after(DateUtils.addMinutes(new Date(), -5))) {
            return MessageBean.fail("处理中订单请于审核成功5分钟后再手动操作");
        }
        //目标状态
        Integer targetStatus = withdrawRecord.getState();
        withdrawRecord.setRemark(withdrawRecord.getRemark());
        withdrawRecord.setOrderId(dbWithdrawRecord.getOrderId());
        withdrawRecord.setUserId(dbWithdrawRecord.getUserId());
        withdrawRecord.setPrice(dbWithdrawRecord.getPrice());
        withdrawRecord.setServicePrice(dbWithdrawRecord.getServicePrice());
        withdrawRecord.setWithdrawChannel(dbWithdrawRecord.getWithdrawChannel());
        withdrawService.modifyWithdraw(withdrawRecord);
        Integer logStatus = 0;
        if (Constants.SUCCESS_STATE.equals(targetStatus)) {
            //置成功
            logStatus = WithdrawLogStatusEnum.DEAL_SUCCESS.getStatus();
        } else if (Constants.FAIL_STATE.equals(targetStatus)) {
            //置失败
            logStatus = WithdrawLogStatusEnum.DEAL_FAIL.getStatus();
        } else if (Constants.EXCEPTION_STATE.equals(targetStatus)) {
            //置异常
            logStatus = WithdrawLogStatusEnum.DEAL_ABNORMAL.getStatus();
        }
        //添加提现操作日志
        WithdrawLog withdrawLog = new WithdrawLog(admin.getName(), withdrawRecord.getId(), logStatus, WithdrawLogTypeEnum.USER_WITHDRAWALS);
        int error = supplyFinanceDao.addWithdrawLog(withdrawLog);
        if (error == 0) {
            log.info("添加提现记录操作失败[{}]", withdrawLog);
        }
        log.info("[{}]操作提现记录[{}]由[{}]处理为[{}]", admin, dbWithdrawRecord, dbWithdrawRecord.getState(), targetStatus);

        return MessageBean.success();
    }

    /**
     * 停止/开启自动开启自动审核获取状态
     *
     * @param status 0:启动  1:停止
     * @return
     */
    @Override
    public MessageBean stopOpenAutoAudit(Integer status) {
        if (Constants.STATE_OPEN.intValue() == status) {
            SimpleCacheUtil.setObject(SupRedisKey.AUTO_AUDIT_STOP_KEY.toString(), SupRedisKey.AUTO_AUDIT_STOP_KEY.toString());
        } else {
            SimpleCacheUtil.removeObject(SupRedisKey.AUTO_AUDIT_STOP_KEY.toString());
        }
        return MessageBean.success();
    }

    /**
     * 停止自动开启自动审核获取状态 true 已停止
     *
     * @return
     */
    @Override
    public boolean getStopOpenAutoAudit() {
        Object autoAudit = SimpleCacheUtil.getObject(SupRedisKey.AUTO_AUDIT_STOP_KEY.toString());
        if (Objects.isNull(autoAudit)) {
            return false;
        }
        return true;
    }

    /**
     * 查询提现记录操作日志
     *
     * @param withdrawId
     * @param type
     * @return
     */
    @Override
    public List<WithdrawLog> queryWithdrawLog(Long withdrawId, WithdrawLogTypeEnum type) {
        return supplyFinanceDao.queryWithdrawLog(withdrawId,type);
    }

    /**
     * 查询会员提现明细
     *
     * @param param 查询参数
     * @return
     */
    @Override
    public List<WithdrawRecordDto> findWithdrawRecord(WithdrawRecordQueryParam param) {
        List<WithdrawRecordDto> list = supplyFinanceDao.findWithdrawRecord(param);
        list.forEach(t -> {
            if (StringUtils.equalsIgnoreCase(Const.ALIPAY, t.getWithdrawChannel())) {
                t.setBankNo(t.getAlipay());
            } else {
                t.setBankNo(WithHoldDes.decodeValue(t.getBankSalt(), t.getBankNo()));
                t.setWithdrawChannel(t.getBankName());
            }
        });
        return list;
    }
}
